import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[]nums = {1,9,2,8,3,7,4,6,5};
        //insertSort(nums);
        shell(nums);
        System.out.println(Arrays.toString(nums));
    }

    private static void shell(int[] array) {
        int gap = array.length;
        while(gap>1){
            gap/=2;
            shellSort(array,gap);
        }
    }

    private static void shellSort(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i-gap;
            for (; j >=0 ; j-=gap) {
                if(array[j]>tmp){
                    array[j+gap] = array[j];
                }else{
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    private static void insertSort(int[] array) {
        for (int i = 1; i < array.length ; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >=0; j--) {
               if(array[j]>tmp){
                   array[j+1] = array[j];
               }else{
                   break;
               }
            }
            array[j+1] = tmp;
        }
    }
}
